import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

//解决重复点击路由出现报错问题
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function(location){
//   return originalPush.call(this,location).catch( err => {});
// };
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component:Home,
    meta:{ 
      activenum:0,
      isshow:true
    },
    children:[
      {
        path: 'searchPopup',
        name: 'SearchPopup',
        meta:{ 
         
          isshow:false
        },
        component: () => import(/* webpackChunkName: "SearchPopup" */ '../views/SearchPopup.vue')
      }
    ]
  },
  {
    path: '/label',
    name: 'Label',
    meta:{ 
      activenum:1,
      isshow:true
    },
    component: () => import(/* webpackChunkName: "Label" */ '../views/Label.vue')
     
  },
  {
    path: '/fenlei',
    name: 'Fenlei',
    meta:{ 
      activenum:2,
      isshow:true
    },
    component: () => import(/* webpackChunkName: "Fenlei" */ '../views/Fenlei.vue')
     
  },{
    path: '/cart',
    name: 'Cart',
    meta:{ 
      activenum:3,
      isshow:true
    },
    component: () => import(/* webpackChunkName: "Cart" */ '../views/Cart.vue')
     
  },
  {
    path: '/my',
    name: 'My',
    meta:{ 
      activenum:4,
      isshow:true
    },
    component: () => import(/* webpackChunkName: "My" */ '../views/My.vue')
     
  },
  {
    path: '/productdetails',
    name: 'ProductDetails',
    meta:{ 
      activenum:5,
      isshow:false
    },
    component: () => import(/* webpackChunkName: "ProductDetails" */ '../views/ProductDetails.vue')
     
  }
  
]

const router = new VueRouter({
  routes
})


//路由守卫
router.beforeEach((to, from, next) => {
   
  let token = localStorage.getItem('token');

  if(to.path=="/cart"){
    if(token){
      next()
    }else{
      Vue.prototype.$toast("请先登录")
      setTimeout(() => {
        next("/my")
      }, 2000);
    }
    // return
  }
  next()
})
export default router
